package com.HW;

import java.util.Arrays;

/**
 * @ClassName : TCountCloseNum
 * @Author : kele
 * @Date: 2023/10/31 23:28
 * @Description :
 */
public class TCountCloseNum {

    public static void main(String[] args) {

//        handle("[50,50,2,3],2");
        handle("[1,1,1,2,1,1,1,1,1],2");
    }

    public static void handle(String str) {

        int[] arr = Arrays.stream(str.replace("[", "").replace("]", "").split(",")).mapToInt(Integer::parseInt).toArray();

        int k = arr[arr.length - 1];

        int[] sort = Arrays.copyOf(arr, arr.length - 1);

        Arrays.sort(sort);
        int tar = sort[sort.length / 2];


        int tmp = Integer.MAX_VALUE;
        int index = 0;
        for (int i = 0; i < arr.length - k; i++) {

            int sum = arr[i];

            for (int j = i+1; j < i + k; j++) {
                sum -= arr[j];
            }

            if (tmp >= Math.abs(sum - tar)) {
                tmp = Math.abs(sum - tar);
                index = i;
            }
        }
        System.out.println(index);

    }
}
